/**   
 * @Title: ScfAssetSideInformationService.java 
 * @Package com.betterjr.modules.loan.service 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuzx
 * @date 2017年11月17日 下午2:26:14  
 */
package com.betterjr.modules.loan.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.agreement.entity.ScfElecAgreement;
import com.betterjr.modules.agreement.service.ScfElecAgreementService;
import com.betterjr.modules.credit.service.ScfCreditService;
import com.betterjr.modules.enquiry.entity.ScfEnquiry;
import com.betterjr.modules.enquiry.service.ScfEnquiryService;
import com.betterjr.modules.loan.entity.ScfAssetSideInformation;
import com.betterjr.modules.loan.entity.ScfRequest;
import com.betterjr.modules.productconfig.sevice.ScfProductConfigService;

/** 
 * @ClassName: ScfAssetSideInformationService 
 * @Description: 资金方工作台信息展示 
 * @author xuzx
 * @date 2017年11月17日 下午2:26:14 
 *  
 */
@Service
public class ScfAssetSideInformationService {

    @Resource
    private ScfCreditService scfCreditService;

    @Resource
    private ScfRequestService scfRequestService;

    @Resource
    private ScfEnquiryService scfEnquiryService;

    @Resource
    private ScfElecAgreementService scfElecAgreementService;

    @Resource
    private ScfProductConfigService scfProductConfigService;

    public ScfAssetSideInformation queryAllAssetInformation(final Long anFactorNo, final boolean isHanding) {
        final ScfAssetSideInformation scfAssetSideInformation = new ScfAssetSideInformation();
        // 登录客户名称
        scfAssetSideInformation.setCustName(UserUtils.getDefCustInfo().getCustName());

        Map<String, Object> creditMap = scfCreditService.queryCapitalPartyCredit();
        // 资金方授信额度，笔数
        scfAssetSideInformation.setScfCreditMap(creditMap);
        // 资金方金额区域信息
        scfAssetSideInformation.setSecuredLoanMap(scfRequestService.queryFactoringAmountArea(anFactorNo));
        // 资金方放款融资信息
        final ScfAssetSideInformation financingLoanInformation = queryfinancingLoan(anFactorNo);
        scfAssetSideInformation.setFinancingLoanCount(financingLoanInformation.getFinancingLoanCount());
        scfAssetSideInformation.setFinancingLoanMoney(financingLoanInformation.getFinancingLoanMoney());
        // 资金方询价信息
        final ScfAssetSideInformation enquiryInformation = queryEnquiryByFactorNo(anFactorNo);
        scfAssetSideInformation.setFinancingInquiryCount(enquiryInformation.getFinancingInquiryCount());
        scfAssetSideInformation.setFinancingInquiryMoney(enquiryInformation.getFinancingInquiryMoney());
        // 资金方授信合同信息
        final ScfAssetSideInformation creditContractInformation = queryCreditContractCountByFactorNo(anFactorNo);
        scfAssetSideInformation.setCreditContractCount(creditContractInformation.getCreditContractCount());
        scfAssetSideInformation.setCreditContractMoney(creditContractInformation.getCreditContractMoney());
        // 资金方融资产品信息
        scfAssetSideInformation
                .setFinancingGoodsCount(scfProductConfigService.queryFinancingProductCount(anFactorNo, isHanding));
        return scfAssetSideInformation;

    }

    /**
     * 
     * @Title: queryfinancingLoan 
     * @Description: 查询资金方融资放款金额，笔数 
     * @return 资金方融资放款金额，笔数 
     * @throws 
     * @author xuzx
     * @date 2017年11月17日 下午5:01:34
     */
    private ScfAssetSideInformation queryfinancingLoan(final Long anFactorNo) {
        final ScfAssetSideInformation information = new ScfAssetSideInformation();
        List<ScfRequest> list = scfRequestService.queryFinancingLoanList(anFactorNo);
        information.setFinancingLoanCount(list.size());
        information.setFinancingLoanMoney(queryfinancingLoanMoney(list));
        return information;

    }

    /**
     * 
     * @Title: queryfinancingLoanMoney 
     * @Description: 计算资金方审批金额
     * @param list 放款中的列表
     * @return 审批金额总数
     * @throws 
     * @author xuzx
     * @date 2017年11月17日 下午5:03:00
     */
    private BigDecimal queryfinancingLoanMoney(final List<ScfRequest> list) {
        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getApprovedBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }

    /**
     * 
     * @Title: queryEnquiryByFactorNo 
     * @Description: 根据资金方编号查询询价笔数，金额
     * @param anFactorNo 资金方编号
     * @return 询价笔数，金额
     * @throws 
     * @author xuzx
     * @date 2017年11月20日 上午10:25:56
     */
    private ScfAssetSideInformation queryEnquiryByFactorNo(final Long anFactorNo) {
        final ScfAssetSideInformation information = new ScfAssetSideInformation();
        List<ScfEnquiry> list = scfEnquiryService.queryEnquiryByfactorNoList(anFactorNo);
        information.setFinancingInquiryCount(list.size());
        information.setFinancingInquiryMoney(queryEnquiryMoney(list));
        return information;

    }

    /**
     * 
     * @Title: queryEnquiryMoney 
     * @Description: 计算询价金额总数 
     * @param list  询价中的列表
     * @return 金额总数 
     * @throws 
     * @author xuzx
     * @date 2017年11月20日 上午10:26:01
     */
    private BigDecimal queryEnquiryMoney(final List<ScfEnquiry> list) {
        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }

    /**
     * 
     * @Title: queryCreditContractCountByFactorNo 
     * @Description: 根据资金方编号查询登记，生效授信合同笔数，金额
     * @param anFactorNo    资金方编号
     * @return 登记，生效授信合同笔数，金额
     * @throws 
     * @author xuzx
     * @date 2017年11月20日 上午11:47:44
     */
    private ScfAssetSideInformation queryCreditContractCountByFactorNo(final Long anFactorNo) {
        final ScfAssetSideInformation information = new ScfAssetSideInformation();
        List<ScfElecAgreement> list = scfElecAgreementService.queryFactoringContractList(anFactorNo);
        information.setCreditContractCount(list.size());
        information.setCreditContractMoney(queryCreditContractMoney(list));
        return information;

    }

    /**
     * 
     * @Title: CreditContractMoney 
     * @Description: 计算询价授信合同总额
     * @param list   登记，生效授信合同列表
     * @return 授信合同总额
     * @throws 
     * @author xuzx
     * @date 2017年11月20日 上午11:48:37
     */
    private BigDecimal queryCreditContractMoney(final List<ScfElecAgreement> list) {
        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }

}
